package com.ciban.smarthome.common.utils;

public class ConvertUtils {
	/**
	 * 将字节数组转换为16进制字符串(大写)
	 * 
	 * @param bytes
	 * @return
	 */
	public static String bytes2hex(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		String tmp = null;
		for (byte b : bytes) {
			tmp = Integer.toHexString(0xFF & b);
			if (tmp.length() == 1) {
				tmp = "0" + tmp;
			}
			sb.append(tmp);
		}
		return sb.toString().toUpperCase();

	}

	/**
	 * 将字节数组转换为16进制字符串(大写)
	 * 
	 * @param bytes
	 * @return
	 */
	public static String bytes2hex(byte[] bytes, int length) {
		StringBuilder sb = new StringBuilder();
		String tmp = null;
		length = length > bytes.length ? bytes.length : length;
		for (int i = 0; i < length; i++) {
			byte b = bytes[i];
			tmp = Integer.toHexString(0xFF & b);
			if (tmp.length() == 1) {
				tmp = "0" + tmp;
			}
			sb.append(tmp);
		}
		return sb.toString().toUpperCase();

	}

	/**
	 * 16进制字符串转 byte
	 * 
	 * @param s
	 * @return
	 */
	public static byte[] stringToByte(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return baKeyword;
	}
	
	/**
	 * 字符串转ascii
	 * 
	 * @param value
	 * @return
	 */
	public static String stringToAscii(String value) {
		StringBuffer sbu = new StringBuffer();
		char[] chars = value.toCharArray();
		int temdInt;
		String str;
		for (int i = 0; i < chars.length; i++) {
			if (i != chars.length - 1) {
				temdInt = (int) chars[i];
				str = Integer.toString(temdInt, 16); // 十六进制
				sbu.append(str);
			} else {
				temdInt = (int) chars[i];
				str = Integer.toString(temdInt, 16); // 十六进制
				sbu.append(str);
			}
		}
		return sbu.toString();
	}

	/**
	 * ascii转String
	 * 
	 * @param text
	 * @return
	 */
	public static String asciiToString(String text) {
		StringBuilder builder = new StringBuilder();
		for (int i = 0; i < text.length(); i++) {
			if (text.charAt(i) <= '2' && i <= text.length() - 3) {
				int code = Integer.parseInt(text.substring(i, i + 3));
				builder.append((char) code);
				i += 2;
			} else {
				int code = Integer.parseInt(text.substring(i, i + 2));
				builder.append((char) code);
				i += 1;
			}
		}
		return builder.toString();
	}

	/**
	 * 10进制串转为BCD码
	 * 
	 * @功能: 10进制串转为BCD码
	 * @参数: 10进制串
	 * @结果: BCD码
	 */
	public static byte[] str2Bcd(String asc) {
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0) {
			asc = "0" + asc;
			len = asc.length();
		}
		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}
		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		int j, k;
		for (int p = 0; p < asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}
			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}
			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}

	/**
	 * int整数转换为4字节的byte数组
	 * 
	 * @param i
	 *            整数
	 * @return byte数组
	 */
	public static byte[] intToByte4(int i) {
		byte[] targets = new byte[4];
		targets[3] = (byte) (i & 0xFF);
		targets[2] = (byte) (i >> 8 & 0xFF);
		targets[1] = (byte) (i >> 16 & 0xFF);
		targets[0] = (byte) (i >> 24 & 0xFF);
		return targets;
	}

	/**
	 * 获取每个字节的每一位
	 * 
	 * @param b
	 * @return
	 */
	public static byte[] getByteArray(byte b) {
		byte[] array = new byte[8];
		for (int i = 7; i >= 0; i--) {
			array[i] = (byte) (b & 0x1);
			b = (byte) (b >> 1);
		}
		return array;
	}
}
